﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;

public partial class Admin_Chucnang : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            {
                Response.Redirect("/Online/Dangnhap.aspx");
            }
            else
            {
                var mcn = (from cn in da.Chucnangs where cn.Tenchucnang == "Quản lý chức năng" select cn.Machucnang).FirstOrDefault();
                var kt = (from a in da.TaikhoanChucnangs where (a.Taikhoan == Session["user"].ToString() && a.Machucnang == mcn) select a).Count();
                if (kt == 1)
                {
                    LoadGV();
                    if (string.IsNullOrEmpty(Request.QueryString["m"]))
                    {
                        btnSua.Enabled = false;
                        btnThem.Enabled = true;
                    }
                    else
                    {
                        int ma = int.Parse(Request.QueryString["m"]);
                        var get = (from x in da.Chucnangs where x.Machucnang == ma select x).FirstOrDefault();
                        txtten.Text = get.Tenchucnang;
                        txtpath.Text = get.Duongdan.Substring(14);
                        icon.Text = get.Icon.Substring(19);
                        btnSua.Enabled = true;
                        btnThem.Enabled = false;
                    }
                }
                else
                {
                    Response.Redirect("/Online/Admin/Default.aspx");
                }
            }
        }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        if (txtten.Text == "" || txtpath.Text == "")
        {
            tb.Text = "<i>Nhập đủ 3 thông tin trên!</i>";
        }
        else
        {
            try
            {

                Chucnang cn = new Chucnang();
                var layma = (from x in da.Chucnangs where x.Machucnang != 0 orderby x.Machucnang descending select x.Machucnang).FirstOrDefault();
                txtmcn.Text = (layma + 1).ToString();
                cn.Machucnang = int.Parse(txtmcn.Text.Trim());
                cn.Tenchucnang = txtten.Text.Trim();
                cn.Duongdan = "/Online/Admin/" + txtpath.Text.Trim();
                cn.Icon = "/Online/Admin/Icon/" + icon.Text.Trim();
                da.Chucnangs.InsertOnSubmit(cn);
                da.SubmitChanges();
                tb.Text = "<i>Thêm bản ghi thành công!</i>";
                LoadGV();
            }
            catch (Exception ax)
            {
                tb.Text = ax.Message;
            }
        }
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        if (txtten.Text == "" || txtpath.Text == "")
        {
            tb.Text = "<i>Nhập đủ 2 thông tin trên!</i>";
        }
        else
        {
            int ma = int.Parse(Request.QueryString["m"]);
            string ico = icon.Text;

            var up = (from p in da.Chucnangs where p.Machucnang == ma select p).Single();
            up.Tenchucnang = txtten.Text.Trim();
            up.Duongdan ="/Online/Admin/"+txtpath.Text.Trim();
            up.Icon ="/Online/Admin/Icon/"+ ico;
            da.SubmitChanges();
            tb.Text = "Cập nhật thành công!";
            LoadGV();
            btnSua.Enabled = false;
            btnThem.Enabled = true;
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        StringCollection idConnllection = new StringCollection();
        string strids = string.Empty;

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("ckq1");
            if (ck != null)
            {
                if (ck.Checked == true)
                {
                    Label lb = (Label)GridView1.Rows[i].Cells[1].FindControl("Ma");
                    strids = lb.Text;
                    idConnllection.Add(strids);
                }
            }
        }
        if (idConnllection.Count > 0)
        {
            string iDS = ""; int a;
            foreach (string id in idConnllection)
            {
                iDS += id + ",";
            }
            string[] mang = iDS.Split(',');
            for (int i = 0; i < mang.Length - 1; i++)
            {
                a = int.Parse(mang[i]);
                var xoa = da.Chucnangs.SingleOrDefault(p => p.Machucnang == a);
                if (xoa != null)
                {
                    da.Chucnangs.DeleteOnSubmit(xoa);
                    da.SubmitChanges();
                }
            }
            tb.Text = "Xóa thành công!";
            LoadGV();
        }
        else
            tb.Text = "Không bản ghi nào được chọn để xóa!";
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Online/Admin/Chucnang.aspx");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        int page = e.NewPageIndex;
        GridView1.PageIndex = page;
        var load = from loadgv in da.Chucnangs where loadgv.Machucnang != 0 select new { loadgv.Machucnang, loadgv.Tenchucnang, Duongdan = loadgv.Duongdan.Substring(14), Icon = loadgv.Icon.Substring(19) };
        GridView1.DataSource = load;
        GridView1.DataBind();
    }
    void LoadGV()
    {
        var load = from loadgv in da.Chucnangs where loadgv.Machucnang != 0 select new { loadgv.Machucnang, loadgv.Tenchucnang, Duongdan = loadgv.Duongdan.Substring(14), Icon = loadgv.Icon.Substring(19) };
        GridView1.DataSource = load;
        GridView1.DataBind();
        txtten.Text = "";
        txtpath.Text = "";
        icon.Text = "";
    }
    
}
